package com.lvyerose.provinceandcity.provinceandcity.adapter;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lvyeRose
 * objective: 俩ListView之间的联系控制器
 * mailbox: lvyerose@163.com
 * time: 15/6/29 22:50
 */
public class ControlAdapter implements AdapterView.OnItemClickListener{
    private Context mContext;
    private ListView letterView;
    private ListView cityView;
    private String [] letter = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    private String [] cityOfletter;
    private String [] cityOfName;
    private List<String> citys = new ArrayList<>();
    private OnItemCityClickListener listener;
    private int letterId,cityId;

    /**
     *
     * @param mContext 上下文对象
     * @param letterView 字母视图listView对象
     * @param cityView 城市视图listView对象
     * @param cityOfletter 城市对应的字母集   城市与该字母集进行一一对应
     * @param cityOfName 城市对应的名字集
     * @param listener 城市列表的点击回调接口
     */
    public ControlAdapter(Context mContext, ListView letterView, ListView cityView, String [] cityOfletter, String [] cityOfName , OnItemCityClickListener listener) {
        this.mContext = mContext;
        this.letterView = letterView;
        this.cityView = cityView;
        this.cityOfletter = cityOfletter;
        this.cityOfName = cityOfName;
        this.listener = listener;

        creatData();
        cityId = cityView.getId();
        letterId = letterView.getId();
        cityView.setAdapter(new CityAdapter(mContext, citys));
        cityView.setOnItemClickListener(this);

        letterView.setAdapter(new LetterAdapter(mContext, letter));
        letterView.setOnItemClickListener(this);
    }

    /**
     * Creat 数据源 根据构筑起传入的俩个集合对应关系进行创建
     */
    private void creatData(){
        String str="";
        for(int i=0;i<letter.length;i++){
            str=letter[i];
            boolean isAddLetter=false;
            for(int j=0;j<cityOfletter.length;j++){
                if(str.equals(cityOfletter[j])){
                    if(!isAddLetter){
                        citys.add(str);
                        isAddLetter=true;
                    }
                    citys.add(cityOfName[j]);
                }
            }
        }
    }






    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            if(parent.getId() == cityId){
                boolean  isLetter=false;
                for(int i=0;i<letter.length;i++){
                    if(letter[i].equals(citys.get(position))){
                        isLetter=true;
                        break;
                    }
                }
                if(!isLetter){

                    listener.clickResult(citys , position);
                }
            }else if(parent.getId() == letterId){
                for(int i=0;i<citys.size();i++){
                    if(letter[position].equals(citys.get(i))){
                        cityView.setSelection(i);
                        break;
                    }
                }
            }
    }

    public interface OnItemCityClickListener{
        void clickResult(List<String> citys , int position);
    }
}
